﻿using Microsoft.Extensions.Configuration;

namespace _02_Configuration
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //创建配置管理器
            ConfigurationManager configuration = new ConfigurationManager();
            configuration.SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("config.json")//添加json文件
                .AddEnvironmentVariables()//添加系统环境变量
                .AddJsonUrl(c =>
                {
                    c.ReloadOnChange = true;
                    c.Url = "http://www.baidu.com";//远程配置
                });
            //打印环境变量
            Console.WriteLine(configuration.GetSection("path").Value);
            //读取josn
            var student = configuration.GetSection("student");
            Console.WriteLine(configuration["student:name"]);
            //类型转换：需要安装Microsoft.Extensions.Configuration.Binder支持
            Console.WriteLine(configuration.GetSection("t1").Get<int>());
            Console.WriteLine(configuration.GetSection("t2").Get<int>());
            //反序列化
            var entry = configuration.GetSection("entry").Get<Entry>();
            Console.WriteLine(student["name"]);
            new TaskCompletionSource().Task.Wait();
        }
    }
    public class Entry
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}